package com.wmh.baseservice.common.utils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author 颜泽贤
 * Date:2017-10-11 下午
 */
public class CommUtil {

    public static String createRandom(boolean numberFlag, int length) {
        String retStr = "";
        String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
        int len = strTable.length();
        boolean bDone = true;
        do {
            retStr = "";
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * len;
                int intR = (int) Math.floor(dblR);
                char c = strTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                retStr += strTable.charAt(intR);
            }
            if (count >= 2) {
                bDone = false;
            }
        } while (bDone);

        return retStr;
    }

    /**
     * 非空验证
     *
     * @return
     * @str 参数列表
     */
    public static boolean checkNull(Object... str) {
        if (null == str) {
            return false;
        }
        for (Object o : str) {
            if (null == o || "".equals(o) || "undefined".equals(o)) {
                return false;
            }
        }
        return true;
    }


    /**
     * 获取客户端真实IP
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = "";
        if (request != null) {
            ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        }
        return ip;
    }


    /**
     * 执行cmd命令
     *
     * @param cmd cmd命令字符串
     * @return 成功或失败
     * @throws InterruptedException
     * @throws ExecutionException
     * @throws IOException
     */
    public static boolean runCmd(String cmd) throws InterruptedException, ExecutionException, IOException {
        LogUtil.info(cmd);
        // 执行cmd命令
        Process process = Runtime.getRuntime().exec(cmd);
        // 读取输入流与错误信息
        InputStream stream = process.getInputStream();
        InputStream error = process.getErrorStream();

        CompletableFuture<String> t1 = CompletableFuture.supplyAsync(() -> {
            BufferedReader br1 = new BufferedReader(new InputStreamReader(stream));
            String line1;
            StringBuilder sb = new StringBuilder();
            try {
                while ((line1 = br1.readLine()) != null) {
                    sb.append(line1).append("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        });

        CompletableFuture<String> t2 = CompletableFuture.supplyAsync(() -> {
            BufferedReader br2 = new BufferedReader(new InputStreamReader(error));
            StringBuilder sb = new StringBuilder();
            try {
                String line2;
                while ((line2 = br2.readLine()) != null) {
                    sb.append(line2).append("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    error.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        });
        process.waitFor();
//        process.destroy();
        int status = process.exitValue();
        LogUtil.info(t1.get());
        if (status != 0) {
            System.err.println(t2.get());
            return false;
        }
        return true;
    }

    public static String subString(String content) {
        if (content.length() > 20) {
            content = content.substring(20);
        }
        return content;
    }

    /**
     * 生成唯一文件名
     * @param originalFilename 原文件名
     * @return 返回唯一文件名
     */
    public static String generateUniqueFileName(String originalFilename) {
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString();
        long timestamp = System.currentTimeMillis();
        return uuid + "_" + timestamp + suffix;
    }

}

